package com.bnyy.gbp.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/6/1
 **/
public class PermissionHelper {
    private static final String TAG = "PermissionHelper";
    public static final int REQUEST_PERMISSION = 1;
    private static Context context;

    public static void init(Context context) {
        PermissionHelper.context = context;
    }

    public static boolean checkPermission(String permission) {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean checkPermission(String... permissions) {
        for (String permission : permissions) {
           if ( ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
               return false;
           }
        }
        return true;
    }

    public static Result getResult(String[] permissions, int[] grantResults) {
        Result result = new Result();

        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                result.setGranted(permission);
            } else {
                result.setDenied(permission);
            }
        }
        return result;
    }

    public static void requestPermission(Fragment fragment, RequestPermission requestPermission) {
        if (requestPermission != null) {
            String[] permissions = requestPermission.permissions();
            if (permissions != null && permissions.length > 0) {
                ArrayList<String> needRequestPermissions = new ArrayList<>();
                ArrayList<String> granted = new ArrayList<>();
                for (String permission : permissions) {
                    if (ContextCompat.checkSelfPermission(fragment.getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
                        needRequestPermissions.add(permission);
                    } else {
                        granted.add(permission);
                    }
                }
                if (granted.size() > 0) {
                    int size = granted.size();
                    String[] strings = new String[size];
                    int[] ints = new int[size];
                    for (int i = 0; i < size; i++) {
                        strings[i] = granted.get(i);
                        ints[i] = PackageManager.PERMISSION_GRANTED;
                    }
                    fragment.onRequestPermissionsResult(REQUEST_PERMISSION, strings, ints);
                }
                if (needRequestPermissions.size() > 0) {
                    String[] target = new String[needRequestPermissions.size()];
                    fragment.requestPermissions(needRequestPermissions.toArray(target), REQUEST_PERMISSION);
                }
            }
        }
    }

    public static PermissionHelper getInstance() {
        if (permissionHelper == null) {
            permissionHelper = new PermissionHelper();
        }
        return permissionHelper;
    }

    private PermissionHelper() {
    }

    private static PermissionHelper permissionHelper;
    private RequestPermission target;

    public void requestPermission(RequestPermission requestPermission) {
        if (target == null) {
            if (requestPermission != null) {
                String[] permissions = requestPermission.permissions();
                if (permissions != null && permissions.length > 0) {
                    target = requestPermission;
                    ArrayList<String> needRequestPermissions = new ArrayList<>();
                    ArrayList<String> granted = new ArrayList<>();
                    for (String permission : permissions) {
                        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                            needRequestPermissions.add(permission);
                        } else {
                            granted.add(permission);
                        }
                    }

                    if (needRequestPermissions.size() <= 0) {
                        if (granted.size() > 0) {
                            int size = granted.size();
                            String[] strings = new String[size];
                            int[] ints = new int[size];
                            for (int i = 0; i < size; i++) {
                                strings[i] = granted.get(i);
                                ints[i] = PackageManager.PERMISSION_GRANTED;
                            }
                            finish(REQUEST_PERMISSION, strings, ints);
                        }
                    } else {
                        if (granted.size() > 0) {
                            target.granted(new HashSet<>(granted));
                        }
                        RequestPermissionActivity.show(context, needRequestPermissions.toArray(new String[needRequestPermissions.size()]));
                    }
                }
            }
        }
    }

    public void requestPermissions(Activity activity, String[] permissions) {
        activity.requestPermissions(permissions, REQUEST_PERMISSION);
    }

    public void finish(int requestCode, String[] permissions, int[] grantResults) {
        if (target != null) {
            if (requestCode == REQUEST_PERMISSION) {
                Result result = getResult(permissions, grantResults);
                int size = result.getGranted().size();
                if (size > 0) {
                    target.granted(result.getGranted());
                    if (size == permissions.length) {
                        target.allPermissionsGranted();
                    }
                }
                if (result.getDenied().size() > 0) {
                    target.denied(result.getDenied());
                }
                target = null;
            }
        }
    }

    public static abstract class RequestPermission {
        public abstract String[] permissions();

        public abstract void granted(HashSet<String> granted);

        public void allPermissionsGranted(){

        }

        public abstract void denied(HashSet<String> denied);
    }

    public static class Result implements Serializable {

        private HashSet<String> granted = new HashSet<>();
        private HashSet<String> denied = new HashSet<>();

        public Result() {

        }

        public void setGranted(String permission) {
            granted.add(permission);
        }

        public void setDenied(String permission) {
            denied.add(permission);
        }

        public HashSet<String> getGranted() {
            return granted;
        }

        public HashSet<String> getDenied() {
            return denied;
        }
    }
}





















